// JavaScript Document
function gotoIndex(){
	window.location.href="/hezi/index";
}

outseq = 0;
inseq=0;
function waitingmessage(queueid){	
	outseq++;
	console.log("---------------------   out【" + outseq + "】: /hezi/message?queueid="+queueid+"&requestseq="+outseq);	
	$.getJSON("/hezi/message?queueid="+queueid+"&requestseq="+outseq,function(data){  
		if(data){
			inseq++;
			console.log("in【" + inseq + "】");
			console.log(data);
			dealmessage(data);
			messagecallback();
		}
		if(needgoon){
			setTimeout("waitingmessage(globalqueueid)",10);
		}
		else{
			console.log("停止waitingmessage");
		}
	});
}

needgoon=true;
function dealmessage(data){
	var jsObject = eval(data);
	if(jsObject){
		for(var i=0;i<jsObject.length;i++)  
		{
			msg = jsObject[i];
			console.log(msg);
			if(msg.msgtype == "end"){
				needgoon = false;
				break;
			}
			else{
				dealmessagebiz(data);
				needgoon = true;
			}
			

		}
	}
}


function dealmessagebiz(data){
			if(msg.method == 'append'){
				//console.log("#"+msg.htmlid + " append: " + msg.childhtml);
				//console.log($("#"+msg.htmlid).html());
				$("#"+msg.htmlid).append(msg.childhtml);
			}	
			else if(msg.method == 'html'){
				$("#"+msg.htmlid).html(msg.htmlcontent);
			}
			else if(msg.method == 'addClass'){
				$("#"+msg.htmlid).addClass(msg.classname);
			}
			else if(msg.method == 'removeClass'){
				$("#"+msg.htmlid).removeClass();
			}
			else if(msg.method == 'removeClassaddClass'){
				$("#"+msg.htmlid).removeClass().addClass(msg.classname);
			}
			else if(msg.method == 'progressBar'){
				$("#"+msg.htmlid).progressBar(msg.precentage);
			}
			else if(msg.method == 'attr'){
				var arr = msg.attrkv.split("=",2);
				console.log(arr[0]);
				console.log(arr[1]);
				$("#"+msg.htmlid).attr(arr[0],arr[1]);
			}
			else if(msg.method == 'css'){
				var arr = msg.attrkv.split("=",2);
				console.log(arr[0]);
				console.log(arr[1]);
				$("#"+msg.htmlid).css(arr[0],arr[1]);
			}
			else if(msg.method == 'toggleandremove'){
				setTimeout("$('#"+msg.htmlid+"').slideToggle(1000)",msg.timeout-1000);
				setTimeout("$('#"+msg.htmlid+"').remove()",msg.timeout);
			}		
			
}

function consolelog(msg) {
	$().toastmessage('showNoticeToast', msg);
}



function onload(){
	var jsonobj = {};
	jsonobj.action = "loadpage";
	jsonobj.element = "";
	sendmsgtoubserver(jsonobj);
}

function onclick(source){
	var jsonobj = {};
	jsonobj.action = "click";
	jsonobj.element = source.val();	
	//jsonobj.context = source.wrapAll('<span></span>').parent().html();
	jsonobj.context = $('<div>').append(source.clone()).html();
	console.log("context",jsonobj.context);
	sendmsgtoubserver(jsonobj);
}

var sendmsg = 1;

function sendmsgtoubserver(jsonobj){
	jsonobj.uid = uid;
	jsonobj.sessionid = sessionid;
	jsonobj.actionseq = actionseq;
	
	jsonobj.actor = "user";
	jsonobj.page = window.location.pathname;

	
	jsonobj.paras = window.location.search;
	jsonobj.time = new Date().format("yyyy-MM-dd hh:mm:ss");
	jsonobj.actiontime = new Date().getTime();
	jsonobj.startuptime = startuptime;
	// JSON.stringify(jsonobj)
	console.log(jsonobj);
	
	console.log("sendmsg:" + sendmsg);
	if(sendmsg){
		
	}
	posttofirebase(jsonobj)
	
}

function posttofirebase(jsonobj){
	var myDataRef = new Firebase('https://luntankong.firebaseIO.com/');
	var queueid = "ub";
	var myqueue = myDataRef.child(queueid);
	myqueue.push(jsonobj);
}

function posttoub(jsonobj){
		$.post("/hezi/ub",
		  { jsonstr: JSON.stringify(jsonobj)
		  },
		  function(returndata){
			  actionseq = returndata;
			  console.log(actionseq);
		});
}


Date.prototype.format = function(format){ 
	var o = { 
	"M+" : this.getMonth()+1, //month 
	"d+" : this.getDate(), //day 
	"h+" : this.getHours(), //hour 
	"m+" : this.getMinutes(), //minute 
	"s+" : this.getSeconds(), //second 
	"q+" : Math.floor((this.getMonth()+3)/3), //quarter 
	"S" : this.getMilliseconds() //millisecond 
	} 
	
	if(/(y+)/.test(format)) { 
	format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
	} 
	
	for(var k in o) { 
	if(new RegExp("("+ k +")").test(format)) { 
	format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); 
	} 
	} 
	return format; 
} 

this.screenshotPreview = function () {
    xOffset = 10;
    yOffset = 30;
    $("a.screenshot").hover(function (e) {
        this.t = this.title;
        this.title = "";
        var c = (this.t != "") ? "<br/>" + this.t : "";
        $("body").append("<p id='screenshot'><img width='600px' src='" + this.rel + "' alt='url preview' />" + c + "</p>");
        $("#screenshot")
   .css("top", (e.pageY - xOffset) + "px")
   .css("left", (e.pageX + yOffset) + "px")
   .fadeIn("fast");
    },
 function () {
     this.title = this.t;
     $("#screenshot").remove();
 });
    $("a.screenshot").mousemove(function (e) {
        $("#screenshot")
   .css("top", (e.pageY - xOffset) + "px")
   .css("left", (e.pageX + yOffset) + "px");
    });
};

